﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace DbFactory
{
    public class dbFactory
    {
        private static SqlSugarClient db;
        private static readonly object locker = new object();
        public static SqlSugarClient Db
        {
            get 
            {
                if (db == null)
                {
                    lock (locker)
                    {
                        if (db == null)
                        {
                            db = new SqlSugarClient(new ConnectionConfig()
                            {
                                ConnectionString = "",//必填, 数据库连接字符串
                                DbType = DbType.MySql,         //必填, 数据库类型
                                IsAutoCloseConnection = true,       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                                InitKeyType = InitKeyType.SystemTable    //默认SystemTable, 字段信息读取, 如：该属性是不是主键，是不是标识列等等信息
                            });
                            //添加Sql打印事件，开发中可以删掉这个代码
                            db.Aop.OnLogExecuting = (sql, pars) =>
                            {
                                Console.WriteLine(sql);
                            };
                        }
                    }
             
                }
                return db;
            }
        }
    }
}
